//
//  CEDurationEventLog.m
//  CELog_Example
//
//  Created by maoerduo on 2021/2/25.
//  Copyright © 2021 maoerduo. All rights reserved.
//

#import "CEDurationEventLog.h"

@interface CEDurationEventLog()

@property (nonatomic, strong) NSMutableDictionary<NSString *, NSString *> * log;

@end

@implementation CEDurationEventLog

- (instancetype)initWithEventName:(NSString *)eventName log:(NSDictionary<NSString *, NSString *> *)log {
    self = [super init];
    if (self) {
        
        self.eventName = eventName;
        self.log = [@{
            @"event_name" : eventName
        } mutableCopy];
        
        [log enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
            self.log[key] = obj;
        }];
        
    }
    return self;
}

- (NSDictionary<NSString *,NSString *> *)content {
    
    NSMutableDictionary * finalLog = [NSMutableDictionary dictionary];
    
    [self.log enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        finalLog[key] = obj;
    }];
    
    return finalLog;
    
}

- (void)start {
    self.startTime = [NSDate date].timeIntervalSince1970;
}

- (void)stop {
    
    NSDate * now = [NSDate date];
    
    self.duration += now.timeIntervalSince1970 - self.startTime;
    
    self.log[@"session_time"] = [NSString stringWithFormat:@"%@",@(self.duration)];
    
}

- (void)pause {
    
    NSDate * now = [NSDate date];
    
    self.duration += now.timeIntervalSince1970 - self.startTime;
    
}

- (void)update:(NSDictionary<NSString *,NSString *> *)log {
    
    [log enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        self.log[key] = obj;
    }];
    
}

@end
